package com.meixiang.activity.homes.service;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.fragment.service.BeautyClinicFragment;
import com.meixiang.fragment.service.GeneTestingFragment;
import com.meixiang.fragment.service.MedicalTourFragment;
import com.meixiang.fragment.service.ReservationFragment;
import com.meixiang.main.BaseActivity;
import com.meixiang.tool.Tool;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author yinsujun  Created at 2016/5/3 16:57
 *         美服务
 */
public class ServiceActivity extends BaseActivity {
    @Bind(R.id.service_tv_appointment)
    TextView serviceTvAppointment;
    @Bind(R.id.service_tv_beauty_clinic)
    TextView serviceTvBeautyClinic;
    @Bind(R.id.service_tv_medical_tour)
    TextView serviceTvMedicalTour;
    @Bind(R.id.service_tv_medical_gene)
    TextView serviceTvMedicalGene;
    @Bind(R.id.iv_position)
    ImageView ivPosition;
    @Bind(R.id.service_frame_content)
    FrameLayout serviceFrameContent;
    FragmentManager manager;
    FragmentTransaction transaction;
    //预约
    ReservationFragment reservation;
    //医美
    BeautyClinicFragment beautyClinic;
    //医旅
    MedicalTourFragment medicalTour;
//    MedicalGeneFragment medicalGene;
    // 基因检测
    GeneTestingFragment meGeneTesting;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_service);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {
        setTitle("美服务");

    }

    @Override
    protected void initView() {
        manager=getSupportFragmentManager();
        switchTab(0);
    }

    @Override
    protected void setData() {

    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @OnClick({R.id.service_tv_appointment, R.id.service_tv_beauty_clinic, R.id.service_tv_medical_tour, R.id.service_tv_medical_gene})
    public void onClick(View view) {
        if (Tool.isFastDoubleClick()) return;
        switch (view.getId()) {
//
            case R.id.service_tv_beauty_clinic:
                switchTab(0);
                break;
            case R.id.service_tv_medical_tour:
                switchTab(1);
                break;
            case R.id.service_tv_medical_gene:
                switchTab(2);
                break;
            case R.id.service_tv_appointment:
                switchTab(3);
                break;

        }
    }
    private void hideFragment(FragmentTransaction transaction) {
        if (reservation != null) {
            transaction.hide(reservation);
        }
        if (beautyClinic != null) {
            transaction.hide(beautyClinic);
        }
        if (medicalTour != null) {
            transaction.hide(medicalTour);
        }
        if (meGeneTesting != null) {
            transaction.hide(meGeneTesting);
        }
    }
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public void switchTab(int index) {
        transaction = manager.beginTransaction();
        hideFragment(transaction);
        switch (index) {

            case 0:
                if (beautyClinic==null){
                    beautyClinic=new BeautyClinicFragment();
                    transaction.add(R.id.service_frame_content,beautyClinic);
                }else{
                    transaction.show(beautyClinic);
                }
//                ivPosition.setTranslationX(ivPosition.getWidth());
                ivPosition.setTranslationX(0);
                break;
            case 1:
                if (medicalTour==null){
                    medicalTour=new MedicalTourFragment();
                    transaction.add(R.id.service_frame_content,medicalTour);
                }else{
                    transaction.show(medicalTour);
                }
//                ivPosition.setTranslationX(ivPosition.getWidth()*2);
                ivPosition.setTranslationX(ivPosition.getWidth());
                break;
            case 2:
                if (meGeneTesting==null){
                    meGeneTesting=new GeneTestingFragment();
                    transaction.add(R.id.service_frame_content,meGeneTesting);
                }else{
                    transaction.show(meGeneTesting);
                }
//                ivPosition.setTranslationX(ivPosition.getWidth()*3);
                ivPosition.setTranslationX(ivPosition.getWidth()*2);
                break;

            case 3:
                if (reservation==null){
                    reservation=new ReservationFragment();
                    transaction.add(R.id.service_frame_content,reservation);
                }else{
                    transaction.show(reservation);
                }
//                ivPosition.setTranslationX(0);
                ivPosition.setTranslationX(ivPosition.getWidth()*3);
                break;
        }
        transaction.commit();
    }

}
